package com.study.zouchao.superproject.comtasklist.fragment;

import android.icu.text.BreakIterator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.MyBaseFragment;
import com.study.zouchao.superproject.comtasklist.fragment.adapter.TaskListAdapter;
import com.study.zouchao.superproject.comtasklist.fragment.presenter.TaskListPresenter;
import com.study.zouchao.superproject.logininterceptor.LoginInterceptor;
import com.study.zouchao.superproject.logininterceptor.LoginInterceptor_C;
import com.study.zouchao.superproject.qustionadd.view.QuestionAddActivity;
import com.study.zouchao.superproject.qustionadd.view.QuestionAddFragment;
import com.study.zouchao.superproject.travellist.TravelContract;
import com.study.zouchao.superproject.travellist.TravelFragment;
import com.study.zouchao.superproject.travellist.TravelPresenter;
import com.study.zouchao.superproject.util.EventBusEvent;
import com.study.zouchao.superproject.util.EventBusEvent_C;
import com.study.zouchao.superproject.util.EventBusUtils;
import com.study.zouchao.superproject.util.FastBlurUtil;
import com.study.zouchao.superproject.util.StringUtils;
import com.study.zouchao.superproject.util.ToastUtils;
import com.study.zouchao.superproject.util.ui.EmptyRecyclerView;
import com.study.zouchao.superproject.util.ui.OnSlideSeeMoreLisenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/20.
 */

public class TaskListFragment extends MyBaseFragment implements ITaskListContract.ITaskListView, SwipeRefreshLayout.OnRefreshListener,  OnSlideSeeMoreLisenter.IOnSlideToBottomListener {
    @BindView(R.id.id_rv_task_list)
    EmptyRecyclerView mRvTaskList;
    @BindView(R.id.id_refreshing_task_list)
    SwipeRefreshLayout mRefreshing;

    @BindView(R.id.id_fab_post)
    FloatingActionButton mFabPost;

    @BindView(R.id.id_rl_empty_view)
    RelativeLayout mRlEmptyView;

    private View mSeeMoreloading;
    private ITaskListContract.ITaskListPresenter mPresenter;


    private static final String KEY_PARAM1 = "KEY_PARAM1";

    public TaskListFragment() {}

    public static TaskListFragment newInstance() {
        TaskListFragment fragment = new TaskListFragment();
        fragment.setArguments(null);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {}
        EventBusUtils.register(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter = new TaskListPresenter(getActivity(), this);
        Log.d("replace>>>", "list");

        init();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventBusEvent event) {
        switch (event.getTag()) {
            case EventBusEvent_C.EVENT_REFRESH_COM_TASK_LIST :
                onRefresh();
                break;
        }
    }

    private void init() {
        mRvTaskList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
        mRvTaskList.setEmptyView(
                mRlEmptyView,
                false, true
        );
        mPresenter.initRecyclerView(mRvTaskList);
        mRefreshing.setOnRefreshListener(this);
        mRvTaskList.addOnScrollListener(new OnSlideSeeMoreLisenter(this));
    }


    @Override
    public void onRefresh() {
        if (mPresenter != null) mPresenter.onRefreshingData();
    }

    @Override
    public void showRefreshingLoading(boolean isShow) {
        if (mRefreshing != null)    mRefreshing.setRefreshing(isShow);
    }

    @Override
    public void showSeeMoreLoading(boolean isShow) {
        if (mSeeMoreloading != null) mSeeMoreloading.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);

    }

    @Override
    public void setSeeMoreLoading(View footerLoadingView) {
        mSeeMoreloading = footerLoadingView;
    }


    @Override
    public void onSeeMore() {
        if (mRefreshing != null) mPresenter.loadSeeMore();
    }

    @OnClick({R.id.id_fab_post})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_fab_post :
                LoginInterceptor.attemptIntent(getActivity(), LoginInterceptor_C.ACTION_QUESTION_ADD_ACTIVITY, null);
//                QuestionAddActivity.actionStartQuestionAddActivity(getActivity());
                break;
        }
    }

    @Override
    protected View getLoadingTargetView() {
        return getCurrentFragmentRootView().findViewById(R.id.id_loading_task_list);
    }

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_task_list;
    }

    @Override
    public void showSnackBar(String title) {
        if (mRvTaskList  == null)   return;
        ToastUtils.showSnackbar(mRvTaskList, title, Snackbar.LENGTH_SHORT);
    }

    @Override
    public void showSnackBar(String title, int showLength) {
        if (mRvTaskList  == null)   return;
        ToastUtils.showSnackbar(mRvTaskList, title, showLength);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtils.unRegister(this);
    }
}
